﻿//准备ExtJS环境
Ext.require
( 
    [
        'Ext.form.field.File',
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.state.*',
        'Ext.form.*'
    ]
);

//挂接环境准备完毕事件
Ext.onReady
( 
    function ()
    {
        try
        {
            //判断是否有菜单
            if ( model_menus.length <= 0 )
            {
                //如果没有，则直接返回
                return;
            }

            //设置状态保存提供者（当前设置为Cookie）
            Ext.state.Manager.setProvider( Ext.create( 'Ext.state.CookieProvider' ) );

            //用于记录展开的面板编号
            var expanded_panel = Ext.util.Cookies.get( 'side_menu_expaned_panel' );

            //用于保存面板
            var panels = [];

            //循环添加
            for ( var index in model_menus )
            {
                //获得模型
                var modal = model_menus[index];

                //创建面板
                var panel = Ext.create
                ( 
                    'Ext.Panel',
                    {
                        title: modal.Title,
                        itemId: modal.ID,
                        autoScroll: true,
                        stateful: false,
                        html: modal.Html,
                        scroll: 'vertical',
                        collapsed: expanded_panel != modal.ID,
                        listeners:
                        {
                            expand: function ( panel, eOpts )
                            {
                                //清除记录
                                Ext.util.Cookies.clear( 'side_menu_expaned_panel' );

                                //记录展开面板的编号
                                Ext.util.Cookies.set( 'side_menu_expaned_panel', panel.itemId );
                            }
                        }
                    }
                );

                //添加面板
                panels.push( panel );
            }

            //创建菜单容器
            var accordion = Ext.create
            ( 
                'Ext.Panel',
                {
                    id: 'side-menu-panel',
                    stateful: false,
                    stateId: 'side-menu-panel-state',
                    renderTo: 'side_menu',
                    width: 159,
                    height: '100%',
                    layout: 'accordion',
                    hideCollapseTool: true,
                    items: panels
                }
            );

            $( window ).bind
            ( 
                "resize",
                function ()
                {
                    //获得容器
                    var container = Ext.get( 'side_menu' );

                    //获得表格控件
                    var panel = Ext.getCmp( 'side-menu-panel' );

                    //判断控件是否存在
                    if ( container && panel )
                    {
                        //设置宽度
                        panel.setHeight( container.getHeight() );
                    }
                }
            );
        }
        catch ( e )
        {
            //提示错误信息
            Ext.MessageBox.show
            ( 
                {
                    title: Common.ApplicationName,
                    msg: e,
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR
                }
            );
        }
    }
);